package com.icesoft.system.controller;

import com.icesoft.framework.core.controller.BaseController;
import com.icesoft.framework.core.dto.AttachMoreDTO;
import com.icesoft.framework.core.entity.ValidList;
import com.icesoft.framework.core.vo.JsonResult;
import com.icesoft.system.permission.OperationCons;
import com.icesoft.system.permission.annotation.BindPermission;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/common")
@RequiredArgsConstructor
@BindPermission(name = "通用接口", develop = true)
public class CommonController extends BaseController {

	@PostMapping("/attachMore")
	@BindPermission(name = "获取附加属性的通用接口", code = OperationCons.CODE_READ, develop = true)
	public JsonResult<?> attachMore(@Valid @RequestBody ValidList<AttachMoreDTO> attachMoreDTOList) throws Exception {
		return JsonResult.OK(super.attachMoreRelatedData(attachMoreDTOList));
	}

	/**
	 * 获取附加属性的通用Options过滤接口，用于前端下拉框选择远程搜索 或 异步加载（树|级联）数据。
	 * <p>
	 * 适用于数据量大时远程过滤获取选项数据，或分层级获取数据
	 *
	 * @param attachMoreDTO
	 * @param parentType
	 * @param parentValue
	 * @return
	 */
	@BindPermission(name = "获取附加属性的通用过滤接口", code = OperationCons.CODE_READ, develop = true)
	@PostMapping({"/attachMoreFilter", "/attachMoreFilter/{parentValue}", "/attachMoreFilter/{parentType}/{parentValue}"})
	public JsonResult<?> attachMoreFilter(@Valid @RequestBody AttachMoreDTO attachMoreDTO,
										  @PathVariable(value = "parentValue", required = false) String parentValue,
										  @PathVariable(value = "parentType", required = false) String parentType) throws Exception {
		return JsonResult.OK(super.attachMoreRelatedData(attachMoreDTO, parentValue, parentType));
	}

	/**
	 * attachMore对象 自定义权限检查点
	 *
	 * @param attachMore
	 * @return 返回true放行
	 */
	@Override
	protected boolean attachMoreSecurityCheck(AttachMoreDTO attachMore) {
		return true;
	}
}
